home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / dino / dino_bot.1 / source / shell / envextr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-10  |  5.4 KB  |  232 lines

  1. /* Copyright, 1990, Regents of the University of Colorado */
  2. /******************************************************************
  3.  *
  4.  *  This program reads a DINO program that has had the comments
  5.  *  stripped out by /lib/cpp and the line information removed by
  6.  *  dinoline and returns the names of all the environment
  7.  *  structures.  It implicitly assumes that the program is not
  8.  *  too badly mangled (it returns the word following each
  9.  *  occurence of "environment").
  10.  *
  11.  ******************************************************************/
  12.  
  13. #include <memory.h>
  14. #include <stdio.h>
  15. #include <errno.h>
  16. #include <fcntl.h>
  17.  
  18. #define BOOL char
  19. #define TRUE 1
  20. #define FALSE 0
  21.  
  22. #define MAXENVS 10
  23. #define MAXNAMESIZE 128
  24.  
  25. static char buffer[2048];
  26. static char *bufptr;
  27. static char *endptr;
  28. static int lastcnt;
  29. static BOOL end = FALSE;
  30. static int fd;
  31.  
  32. void refreshbuf();
  33.  
  34. extern void exit();
  35. extern char *strncpy();
  36.  
  37. main(argc, argv)
  38.     int argc;
  39.     char *argv[];
  40.    {
  41.     int I;
  42.     char envs[MAXENVS][MAXNAMESIZE];
  43.     int nenvs = 0;
  44.     int bytes;
  45.     char *temp;
  46.  
  47.         /* Open the source file for reading. */
  48.  
  49.     if ((fd = open(argv[1], O_RDONLY)) == -1)
  50.        {
  51.         perror("Environment extraction");
  52.         exit(1);
  53.        }
  54.  
  55.         /* Fill the read buffer and set the pointers. */
  56.  
  57.     bytes = read(fd, buffer, 2048);
  58.     if (bytes < 0)
  59.        {
  60.         perror("Environment extraction");
  61.         exit(1);
  62.        }
  63.     bufptr = buffer;
  64.     if (bytes < 1024)
  65.        {
  66.         endptr = buffer + bytes;
  67.         end = TRUE;
  68.        }
  69.     else
  70.        {
  71.         endptr = buffer + 1024;
  72.         lastcnt = bytes - 1024;
  73.        }
  74.  
  75.         /* Loop for as long as there is more than
  76.                                       can be put into the read buffer. */
  77.  
  78.     for(;;)
  79.        {
  80.             /* Look for an "e". */
  81.  
  82.         for (;bufptr != endptr && *bufptr != 'e'; bufptr++);
  83.  
  84.             /* If we reached the halfway point in the buffer, refill it. */
  85.  
  86.         if (bufptr == endptr)
  87.            {
  88.             if (end)
  89.                 break;
  90.             else
  91.                {
  92.                 refreshbuf();
  93.                 continue;
  94.                }
  95.            }
  96.  
  97.             /* Check that the character after the "e"
  98.                                          is not at the halfway point. */
  99.  
  100.         if (++bufptr == endptr)
  101.            {
  102.             if (end)
  103.                 break;
  104.             else
  105.                 refreshbuf();
  106.            }
  107.  
  108.             /* If the character after the "e" is not an "n",
  109.                                   go back to the beginning of the loop. */
  110.  
  111.         if (*bufptr != 'n')
  112.             continue;
  113.  
  114.             /* Check that the character after the "n"
  115.                                          is not at the halfway point. */
  116.  
  117.         if (++bufptr == endptr)
  118.            {
  119.             if (end)
  120.                 break;
  121.             else
  122.                 refreshbuf();
  123.            }
  124.  
  125.             /* If the character after the "n" is not a "v",
  126.                                   go back to the beginning of the loop. */
  127.  
  128.         if (*bufptr != 'v')
  129.             continue;
  130.  
  131.             /* Check that the character after the "v"
  132.                                          is not at the halfway point. */
  133.  
  134.         if (++bufptr == endptr)
  135.            {
  136.             if (end)
  137.                 break;
  138.             else
  139.                 refreshbuf();
  140.            }
  141.  
  142.             /* Look to see if the rest of
  143.                                     "environment" follows the "env". */
  144.  
  145.         if (memcmp(bufptr, "ironment", 8) != 0)
  146.             continue;
  147.  
  148.             /* If so, move the pointer past the "environment" and if
  149.                   we have past the halfway point, refresh the buffer. */
  150.  
  151.         bufptr += 8;
  152.         if (bufptr >= endptr)
  153.            {
  154.             if (end)
  155.                 break;
  156.             else
  157.                 refreshbuf();
  158.            }
  159.  
  160.             /* Skip all spaces, tabs, and newlines. */
  161.  
  162.         for(;;)
  163.            {
  164.             bufptr++;
  165.             if (bufptr == endptr)
  166.                {
  167.                 if (end)
  168.                     break;
  169.                 else
  170.                     refreshbuf();
  171.                }
  172.             if (*bufptr != ' ' && *bufptr != '\t' && *bufptr != '\n')
  173.                 break;
  174.            }
  175.         if (bufptr == endptr)
  176.             break;
  177.  
  178.             /* Find the end of the word after "environment". */
  179.  
  180.         for(temp = bufptr;;)
  181.            {
  182.             temp++;
  183.             if (*temp == ' ' || *temp == '\t' || (temp == endptr && end) ||
  184.                                 *temp == '\n' || *temp == '[' || *temp == '{')
  185.                 break;
  186.            }
  187.  
  188.             /* Copy that word into the env table. */
  189.  
  190.         (void) strncpy(envs[nenvs], bufptr, temp - bufptr);
  191.         envs[nenvs++][temp - bufptr] = '\0';
  192.  
  193.             /* Reset the buffer pointer. */
  194.  
  195.         bufptr = temp;
  196.         if (bufptr >= endptr)
  197.            {
  198.             if (end)
  199.                 break;
  200.             else
  201.                 refreshbuf();
  202.            }
  203.        }
  204.  
  205.         /* Print out the env table. */
  206.  
  207.     for (I = 0; I < nenvs; I++)
  208.        {
  209.         (void) printf("%s", envs[I]);
  210.         if (I < nenvs - 1)
  211.             (void) printf(" ");
  212.        }
  213.  
  214.     (void) close(fd);
  215.  
  216.     return(0);
  217.    }
  218.  
  219. void refreshbuf()
  220.    {
  221.     bufptr -= 1024;
  222.     (void) memcpy(buffer, endptr, lastcnt);
  223.     if (lastcnt < 1024)
  224.        {
  225.         end = TRUE;
  226.         endptr = buffer + lastcnt;
  227.        }
  228.     else
  229.         lastcnt = read(fd, endptr, 1024);
  230.    }
  231.  
  232.